﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package reprise.ui.renderers {
    import flash.events.*;
    import reprise.external.*;
    import reprise.commands.*;
    import reprise.css.*;
    import reprise.events.*;
    import flash.display.*;
    import flash.geom.*;
    import reprise.data.*;
    import reprise.utils.*;
    import flash.filters.*;
    import reprise.css.transitions.*;
    import reprise.css.propertyparsers.*;

    public class DefaultBackgroundRenderer extends AbstractCSSRenderer {

        protected var m_inactiveBackgroundAnimationContainer:Sprite;
        protected var m_backgroundMask:Sprite;
        protected var m_backgroundImageContainer:Sprite;
        protected var m_animationControls:CompositeCommand;
        protected var m_lastBackgroundImageType:String;
        protected var m_activeBackgroundAnimationContainer:Sprite;
        protected var m_lastBackgroundImageURL:String;
        protected var m_backgroundImageLoader:AbstractResource;
        protected var m_backgroundImage:BitmapData;// = null

        protected function clearBackgroundImage():void{
            if (m_backgroundImageContainer != null){
                m_backgroundImageContainer.parent.removeChild(m_backgroundImageContainer);
                m_backgroundImageContainer = null;
                m_activeBackgroundAnimationContainer = null;
                m_inactiveBackgroundAnimationContainer = null;
            };
            if (m_backgroundImage != null){
                m_backgroundImage.dispose();
                m_backgroundImage = null;
            };
        }
        protected function drawScale9RepeatedBackground(_arg1:BitmapData, _arg2:Rectangle, _arg3:Boolean=false):void{
            var _local4:Object = GfxUtil.segmentedBitmapsOfScale9RectInRectWithSize(_arg1, _arg2);
            var _local5:Matrix = new Matrix();
            var _local6 = !((m_styles.backgroundImageAliasing == Background.IMAGE_ALIASING_ALIAS));
            _local5.translate(0, 0);
            m_backgroundImageContainer.graphics.beginBitmapFill(_local4.tl, _local5, false, _local6);
            GfxUtil.drawRect(m_backgroundImageContainer, _local5.tx, _local5.ty, _local4.tl.width, _local4.tl.height);
            _local5.tx = _local4.tl.width;
            _local5.ty = 0;
            m_backgroundImageContainer.graphics.beginBitmapFill(_local4.t, _local5, true, _local6);
            GfxUtil.drawRect(m_backgroundImageContainer, _local5.tx, _local5.ty, ((m_width - _local4.tl.width) - _local4.tr.width), _local4.t.height);
            _local5.tx = (m_width - _local4.tr.width);
            _local5.ty = 0;
            m_backgroundImageContainer.graphics.beginBitmapFill(_local4.tr, _local5, false, _local6);
            GfxUtil.drawRect(m_backgroundImageContainer, _local5.tx, _local5.ty, _local4.tr.width, _local4.tr.height);
            _local5.tx = (m_width - _local4.r.width);
            _local5.ty = _local4.tr.height;
            m_backgroundImageContainer.graphics.beginBitmapFill(_local4.r, _local5, true, _local6);
            GfxUtil.drawRect(m_backgroundImageContainer, _local5.tx, _local5.ty, _local4.r.width, ((m_height - _local4.tr.height) - _local4.br.height));
            _local5.tx = (m_width - _local4.br.width);
            _local5.ty = (m_height - _local4.br.height);
            m_backgroundImageContainer.graphics.beginBitmapFill(_local4.br, _local5, false, _local6);
            GfxUtil.drawRect(m_backgroundImageContainer, _local5.tx, _local5.ty, _local4.br.width, _local4.br.height);
            _local5.tx = _local4.bl.width;
            _local5.ty = (m_height - _local4.b.height);
            m_backgroundImageContainer.graphics.beginBitmapFill(_local4.b, _local5, true, _local6);
            GfxUtil.drawRect(m_backgroundImageContainer, _local5.tx, _local5.ty, ((m_width - _local4.bl.width) - _local4.br.width), _local4.b.height);
            _local5.tx = 0;
            _local5.ty = (m_height - _local4.bl.height);
            m_backgroundImageContainer.graphics.beginBitmapFill(_local4.bl, _local5, false, _local6);
            GfxUtil.drawRect(m_backgroundImageContainer, _local5.tx, _local5.ty, _local4.bl.width, _local4.bl.height);
            _local5.tx = 0;
            _local5.ty = _local4.tl.height;
            m_backgroundImageContainer.graphics.beginBitmapFill(_local4.l, _local5, true, _local6);
            GfxUtil.drawRect(m_backgroundImageContainer, _local5.tx, _local5.ty, _local4.l.width, ((m_height - _local4.tl.height) - _local4.bl.height));
            _local5.tx = _local4.tl.width;
            _local5.ty = _local4.tl.height;
            m_backgroundImageContainer.graphics.beginBitmapFill(_local4.c, _local5, true, _local6);
            GfxUtil.drawRect(m_backgroundImageContainer, _local5.tx, _local5.ty, ((m_width - _local4.l.width) - _local4.r.width), ((m_height - _local4.t.height) - _local4.b.height));
            m_backgroundImageContainer.graphics.endFill();
        }
        override public function draw():void{
            var _local4:Gradient;
            var _local5:DropShadowFilter;
            var _local1:Boolean;
            var _local2:AdvancedColor = m_styles.backgroundColor;
            var _local3:Boolean = (((((m_styles.backgroundGradientType == Background.GRADIENT_TYPE_LINEAR)) || ((m_styles.backgroundGradientType == Background.GRADIENT_TYPE_RADIAL)))) && (m_styles.backgroundGradientColors));
            m_display.graphics.clear();
            if (((((!((_local2 == null))) && ((_local2.alpha() >= 0)))) && (!(_local3)))){
                _local1 = true;
                m_display.graphics.beginFill(_local2.rgb(), _local2.opacity());
                GfxUtil.drawRect(m_display, 0, 0, m_width, m_height);
                m_display.graphics.endFill();
            } else {
                if (_local3){
                    _local1 = true;
                    _local4 = new Gradient(m_styles.backgroundGradientType);
                    _local4.setColors(m_styles.backgroundGradientColors);
                    if (m_styles.backgroundGradientRatios){
                        _local4.setRatios(m_styles.backgroundGradientRatios);
                    };
                    if (m_styles.backgroundGradientRotation != 0){
                        _local4.setRotation(m_styles.backgroundGradientRotation);
                    };
                    _local4.beginGradientFill(m_display.graphics, m_width, m_height);
                    GfxUtil.drawRect(m_display, 0, 0, m_width, m_height);
                    m_display.graphics.endFill();
                };
            };
            if (m_styles.backgroundImage != Background.IMAGE_NONE){
                _local1 = true;
                loadBackgroundImage();
            } else {
                if (((m_backgroundImageLoader) && (m_backgroundImageLoader.isExecuting()))){
                    m_backgroundImageLoader.cancel();
                };
                m_backgroundImageLoader = null;
                clearBackgroundImage();
            };
            if (m_styles.backgroundShadowColor != null){
                _local1 = true;
                _local5 = Filters.dropShadowFilterFromStyleObjectForName(m_styles, "background");
                m_display.filters = [_local5];
            } else {
                m_display.filters = [];
            };
            if (_local1){
                drawBackgroundMask();
            } else {
                m_display.mask = null;
            };
        }
        protected function loadBackgroundImage():void{
            if (((!(m_styles.backgroundImageType)) || (!((m_styles.backgroundImageType == "animation"))))){
                if (((((m_backgroundImageLoader) && ((m_backgroundImageLoader is ImageResource)))) && (m_backgroundImageLoader.isExecuting()))){
                    m_backgroundImageLoader.cancel();
                };
                if (((((m_backgroundImageLoader) && ((m_backgroundImageLoader is BitmapResource)))) && ((m_backgroundImageLoader.url() == m_styles.backgroundImage)))){
                    if (!m_backgroundImageLoader.isExecuting()){
                        m_backgroundImageLoader.dispatchEvent(new ResourceEvent(Event.COMPLETE, true));
                    };
                    return;
                };
                if (((m_backgroundImageLoader) && (m_backgroundImageLoader.isExecuting()))){
                    m_backgroundImageLoader.cancel();
                };
                m_backgroundImageLoader = new BitmapResource();
                m_backgroundImageLoader.setURL(m_styles.backgroundImage);
                BitmapResource(m_backgroundImageLoader).setCacheBitmap(true);
                BitmapResource(m_backgroundImageLoader).setCloneBitmap(false);
                m_backgroundImageLoader.addEventListener(Event.COMPLETE, bitmapLoader_complete);
                m_backgroundImageLoader.execute();
                return;
            };
            if (((((m_backgroundImageLoader) && (m_backgroundImageLoader.isExecuting()))) && ((m_backgroundImageLoader is BitmapResource)))){
                m_backgroundImageLoader.cancel();
                m_backgroundImageLoader.removeEventListener(Event.COMPLETE, bitmapLoader_complete);
                m_backgroundImageLoader = null;
            };
            if (!m_backgroundImageContainer){
                m_backgroundImageContainer = Sprite(m_display.addChildAt(new Sprite(), 0));
                m_backgroundImageContainer.name = "m_backgroundImageContainer";
            };
            if (((((m_backgroundImageLoader) && ((m_backgroundImageLoader.url() == m_styles.backgroundImage)))) && ((m_backgroundImageLoader is ImageResource)))){
                if (((m_backgroundImageLoader.didFinishLoading()) && (m_backgroundImageLoader.didSucceed()))){
                    m_backgroundImageLoader.dispatchEvent(new ResourceEvent(Event.COMPLETE, true));
                };
                return;
            };
            if (!m_backgroundImageContainer){
                m_backgroundImageContainer = Sprite(m_display.addChildAt(new Sprite(), 0));
                m_backgroundImageContainer.name = "m_backgroundImageContainer";
            };
            m_inactiveBackgroundAnimationContainer = m_activeBackgroundAnimationContainer;
            m_activeBackgroundAnimationContainer = Sprite(m_backgroundImageContainer.addChild(new Sprite()));
            m_activeBackgroundAnimationContainer.name = "m_backgroundAnimation";
            m_backgroundImageLoader = new ImageResource();
            m_backgroundImageLoader.setURL(m_styles.backgroundImage);
            m_backgroundImageLoader.addEventListener(Event.COMPLETE, imageLoader_complete);
            m_backgroundImageLoader.execute();
        }
        protected function imageLoader_complete(_arg1:ResourceEvent=null):void{
            if (((((!(_arg1.success)) || ((m_styles.backgroundImage == null)))) || ((m_styles.backgroundImage == Background.IMAGE_NONE)))){
                clearBackgroundImage();
                return;
            };
            if (m_inactiveBackgroundAnimationContainer){
                m_inactiveBackgroundAnimationContainer.parent.removeChild(m_inactiveBackgroundAnimationContainer);
                m_inactiveBackgroundAnimationContainer = null;
            };
            var _local2:Sprite = m_activeBackgroundAnimationContainer;
            _local2.addChild(m_backgroundImageLoader.content());
            if ((((m_backgroundImageLoader.content() is MovieClip)) && (m_styles.backgroundAnimationControl))){
                applyAnimationControls();
            };
            var _local3:Number = _local2.width;
            var _local4:Number = _local2.height;
            var _local5:Rectangle = constructScale9Rect(_local3, _local4);
            if (_local5 != null){
                _local2.x = (_local2.y = 0);
                _local2.scale9Grid = _local5;
                _local2.width = m_width;
                _local2.height = m_height;
                m_backgroundImageContainer.graphics.clear();
                return;
            };
            _local2.scale9Grid = null;
            _local2.scaleX = (_local2.scaleY = 1);
            var _local6:Point = new Point((m_styles.backgroundPositionX | 0), (m_styles.backgroundPositionY | 0));
            var _local7:CSSProperty = m_complexStyles.getStyle("backgroundPositionX");
            if (((_local7) && (_local7.isRelativeValue()))){
                _local6.x = Math.round(_local7.resolveRelativeValueTo((m_width - _local3)));
            };
            var _local8:CSSProperty = m_complexStyles.getStyle("backgroundPositionY");
            if (((_local8) && (_local8.isRelativeValue()))){
                _local6.y = Math.round(_local8.resolveRelativeValueTo((m_height - _local4)));
            };
            _local2.x = _local6.x;
            _local2.y = _local6.y;
            m_backgroundImageContainer.graphics.clear();
        }
        protected function applyAnimationControls():void{
            var _local4:Object;
            var _local5:CSSMovieClipController;
            if (m_animationControls){
                m_animationControls.cancel();
            };
            m_animationControls = new CompositeCommand();
            var _local1:Array = m_styles.backgroundAnimationControl;
            var _local2:MovieClip = m_backgroundImageLoader.content();
            var _local3:int;
            while (_local3 < _local1.length) {
                _local4 = _local1[_local3];
                _local5 = new CSSMovieClipController(_local2);
                _local5.setOperation(_local4.type, _local4.parameters);
                m_animationControls.addCommand(_local5);
                _local3++;
            };
            m_animationControls.execute();
        }
        protected function drawBackgroundMask():void{
            var _local5:Number;
            var _local1:Array = [];
            var _local2:Boolean;
            var _local3:Array = ["borderTopLeftRadius", "borderTopRightRadius", "borderBottomRightRadius", "borderBottomLeftRadius"];
            var _local4:Number = (((m_styles["borderTopWidth"]) || (0)) / 2);
            var _local6:int;
            while (_local6 < _local3.length) {
                if (!(m_styles[_local3[_local6]] is Number)){
                    _local5 = 0;
                } else {
                    _local5 = m_styles[_local3[_local6]];
                };
                if (_local5 != 0){
                    _local5 = (_local5 + _local4);
                    _local2 = true;
                };
                _local1.push(_local5);
                _local6++;
            };
            if (!_local2){
                m_display.mask = null;
                GfxUtil.drawRect(m_display, 0, 0, m_width, m_height);
            } else {
                if (!m_backgroundMask){
                    m_backgroundMask = new Sprite();
                    m_display.addChild(m_backgroundMask);
                    m_backgroundMask.name = "mask";
                };
                m_display.mask = m_backgroundMask;
                m_backgroundMask.graphics.clear();
                m_backgroundMask.graphics.beginFill(0xFFFF, 20);
                GfxUtil.drawRoundRect(m_backgroundMask, 0, 0, m_width, m_height, _local1);
            };
        }
        protected function constructScale9Rect(_arg1:Number, _arg2:Number):Rectangle{
            if ((((((((((((m_styles.backgroundScale9Type == null)) || ((m_styles.backgroundScale9Type == Background.SCALE9_TYPE_NONE)))) || ((m_styles.backgroundScale9RectTop == null)))) || ((m_styles.backgroundScale9RectRight == null)))) || ((m_styles.backgroundScale9RectBottom == null)))) || ((m_styles.backgroundScale9RectLeft == null)))){
                return (null);
            };
            var _local3:Rectangle = new Rectangle();
            _local3.top = m_styles.backgroundScale9RectTop;
            _local3.left = m_styles.backgroundScale9RectLeft;
            _local3.width = ((_arg1 - m_styles.backgroundScale9RectRight) - m_styles.backgroundScale9RectLeft);
            _local3.height = ((_arg2 - m_styles.backgroundScale9RectTop) - m_styles.backgroundScale9RectBottom);
            return (_local3);
        }
        override public function destroy():void{
            ((m_backgroundImageLoader) && (m_backgroundImageLoader.cancel()));
        }
        protected function bitmapLoader_complete(_arg1:ResourceEvent):void{
            var _local13:BitmapData;
            clearBackgroundImage();
            if (((((((!(_arg1.success)) || ((m_styles.backgroundImage == null)))) || ((m_styles.backgroundImage == Background.IMAGE_NONE)))) || (!(m_backgroundImageLoader.content())))){
                return;
            };
            if ((!(m_backgroundImageLoader.content()) is BitmapData)){
                return;
            };
            var _local2:BitmapData = BitmapData(m_backgroundImageLoader.content());
            var _local3:Number = _local2.width;
            var _local4:Number = _local2.height;
            if ((((_local3 < 1)) || ((_local4 < 1)))){
                return;
            };
            if (!m_backgroundImageContainer){
                m_backgroundImageContainer = Sprite(m_display.addChildAt(new Sprite(), 0));
                m_backgroundImageContainer.name = "m_backgroundImageContainer";
            };
            var _local5:String = m_styles.backgroundRepeat;
            var _local6:Point = new Point((m_styles.backgroundPositionX | 0), (m_styles.backgroundPositionY | 0));
            var _local7:CSSProperty = m_complexStyles.getStyle("backgroundPositionX");
            if (((_local7) && (_local7.isRelativeValue()))){
                _local6.x = Math.round(_local7.resolveRelativeValueTo((m_width - _local3)));
            };
            var _local8:CSSProperty = m_complexStyles.getStyle("backgroundPositionY");
            if (((_local8) && (_local8.isRelativeValue()))){
                _local6.y = Math.round(_local8.resolveRelativeValueTo((m_height - _local4)));
            };
            var _local9:Rectangle = constructScale9Rect(_local3, _local4);
            if (_local9 != null){
                if (m_styles.backgroundScale9Type == Background.SCALE9_TYPE_REPEAT){
                    drawScale9RepeatedBackground(_local2, _local9);
                    return;
                };
                _local13 = new BitmapData(m_width, m_height, true, 0);
                GfxUtil.scale9Bitmap(_local2, _local13, _local9);
                _local5 = Background.REPEAT_NO_REPEAT;
                _local6 = new Point(0, 0);
                _local2 = _local13;
                _local3 = m_width;
                _local4 = m_height;
            };
            var _local10:Rectangle = new Rectangle(0, 0, m_width, m_height);
            var _local11:Matrix = new Matrix();
            _local11.translate(_local6.x, _local6.y);
            switch (_local5){
                case Background.REPEAT_REPEAT_XY:
                case undefined:
                    break;
                case Background.REPEAT_REPEAT_X:
                    _local10.top = _local6.y;
                    _local10.height = _local4;
                    break;
                case Background.REPEAT_REPEAT_Y:
                    _local10.left = _local6.x;
                    _local10.width = _local3;
                    break;
                case Background.REPEAT_NO_REPEAT:
                    _local10.topLeft = _local6;
                    _local10.size = new Point(_local3, _local4);
                    break;
            };
            _local10.top = Math.max(0, _local10.top);
            _local10.left = Math.max(0, _local10.left);
            _local10.right = Math.min(m_width, _local10.right);
            _local10.bottom = Math.min(m_height, _local10.bottom);
            var _local12 = !((m_styles.backgroundImageAliasing == Background.IMAGE_ALIASING_ALIAS));
            m_backgroundImageContainer.graphics.beginBitmapFill(_local2, _local11, true, _local12);
            GfxUtil.drawRect(m_backgroundImageContainer, _local10.left, _local10.top, _local10.width, _local10.height);
            m_backgroundImageContainer.graphics.endFill();
        }

    }
}//package reprise.ui.renderers 
